home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / Watcher / Cyylex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-14  |  1.9 KB  |  98 lines

  1. /*
  2.    yylex for watcher: this is a simple routine looking for numbers,
  3.    special characters and strings.  The special chars are stored in
  4.    'words' and represent tokens by themselves.   In y.tab.h are the
  5.    values to return for the various tokens which are not listed in
  6.    'words'.
  7.  
  8.    Kenneth Ingham
  9.  
  10.    Copyright (C) 1987 The University of New Mexico
  11. */
  12.  
  13. #include "defs.h"
  14. #include "y.tab.h"
  15.  
  16. char words[] = "\".,*|;:%@$-{}";
  17.  
  18. yylex()
  19. {
  20.     int c, i;
  21.     static char str[MAX_STR];
  22.     int real;
  23.  
  24.     while (isspace(c = getchar()))
  25.         ;
  26.  
  27.     if (c == EOF)
  28.         return EOF;
  29.  
  30.     if (c == '(') { /* aha, pipeline */
  31.         c = getchar();
  32.         for (i=0; c != EOF && c != ')'; i++) {
  33.             str[i] = c;
  34.             c = getchar();
  35.         }
  36.         str[i] = '\0';
  37.         if (c == EOF) {
  38.             fprintf(stderr, "Missing ')' to end pipeline.\n");
  39.             return EOF;
  40.         }
  41.         yylval.str = strsave(str);
  42.         return PIPELINE;
  43.     }
  44.  
  45.     if (c == '#') { /* comment to end of line */
  46.         while (c != '\n' && c != EOF)
  47.             c = getchar();
  48.         if (c == EOF)
  49.             return EOF;
  50.         return yylex();
  51.     }
  52.  
  53.     if (index(words, c) != 0)
  54.         return c;
  55.  
  56.     if (c == '+' || c == '-' || isdigit(c)) { /* a number */
  57.         real = False;
  58.         i = 0;
  59.         str[i++] = c;
  60.         do {
  61.             str[i++] = getchar();
  62.             if (str[i-1] == '.') 
  63.                 real = True;
  64.         } while (isdigit(str[i-1]) || str[i-1] == '.');
  65.         (void) ungetchar(str[i-1]);
  66.         str[i-1] = '\0';
  67.         if (real) {
  68.             yylval.real = (float) atof(str);
  69.             return FLOAT;
  70.         }
  71.         else {
  72.             yylval.integer = atoi(str);
  73.             return INTEGER;
  74.         }
  75.     }
  76.  
  77.     if (c == '\'') { /* literal string */
  78.         c = getchar();
  79.         for (i=0; c != EOF && c != '\''; i++) {
  80.             str[i] = c;
  81.             c = getchar();
  82.         }
  83.         str[i] = '\0';
  84.         yylval.str = strsave(str);
  85.         return STRING;
  86.     }
  87.  
  88.     /* nothing else matched.  Must be plain string (whitespace sep) */
  89.     for (i=1, str[0]=c; c != EOF && !isspace(c) && !index(words,c); i++) {
  90.         c = getchar();
  91.         str[i] = c;
  92.     }
  93.     (void) ungetchar(c);
  94.     str[i-1] = '\0';
  95.     yylval.str = strsave(str);
  96.     return STRING;
  97. }
  98.